//
//  CardCenterView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/11.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol CardCenterDelegate {
    
    @objc optional func selectBankType(title:String,imageUrl:String) //选择银行信用卡
    @objc optional func moreBankCard(title:String) //更多银行信用卡
}

class CardCenterView: UIView {
    
    weak var cardDelegate : CardCenterDelegate?
    
    fileprivate let cellId = "centerCell"
    
    //信用卡
    let bankArray = [CITICBank,PufaBank,GuangfaBank,EverbrightBank,PeaceBank,CitiBank,GuangzhouBank ,ZheshangBank]
    //更多信用卡
    let moreBankArray = [CITICBank,PufaBank,GuangfaBank,EverbrightBank,PeaceBank,CitiBank,GuangzhouBank,ZheshangBank,ChinaMerchantsBank,BankofCommunications,MinshengBank,IndustrialBank,HSBCBank,ShanghaiBank,BankofChina,ABC,ICBC,Constructionbank,Postalsaving,BaoshangBank,BankofBeijing,ChongqingBank,HSBC,StandardCharteredBank]
    
    //图片
    let images = ["zhongxin","pufa","guangfa","guangda","pingan","huaqi","guangzhou","zheshang","zhaoshang","jiaotong","minsheng","xingye","huaxia","shanghai","boc","nongye","gongshang","jianshe","youzheng","baoshang","beijing","chongqing","huifeng","zhada"]
    
    var dataArray = [String]()
    
    fileprivate let titleLabel = UILabel()
    
    fileprivate let moreButton = UIButton()
    
    var collectionView : UICollectionView!
    

    init() {
        super.init(frame: .zero)
        
        self.dataArray = bankArray
        
        titleLabel.text = popularBank + cardCenter
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        self.addSubview(titleLabel)
        
        let grayView = UIView()
        grayView.backgroundColor = UIColor.lineColor()
        self.addSubview(grayView)
        
        moreButton.setTitle(moreBankCardCenter, for: .normal)
        moreButton.setTitleColor(UIColor.gray, for: .normal)
        moreButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        moreButton.addTarget(self, action: #selector(loadMoreAction(sender:)), for: .touchUpInside)
        self.addSubview(moreButton)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: ScreenW/2, height: RATIO_H(maxNum: 40))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(SelectBankCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 8))
            make.right.equalToSuperview().offset(RATIO(maxNum: -8))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        grayView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.bottom.equalTo(titleLabel)
        }
        
        moreButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(RATIO_H(maxNum: -10))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom)
            make.bottom.equalTo(moreButton.snp.top)
        }
        
        
    }
    
    //MARK: 点击加载
    @objc fileprivate func loadMoreAction(sender:UIButton) {
        
        if sender.titleLabel?.text == bankCardCenter {
            sender.setTitle(moreBankCardCenter, for: .normal)
            self.dataArray = bankArray
        }else{
            sender.setTitle(bankCardCenter, for: .normal)
            self.dataArray = moreBankArray
        }
        
        self.collectionView.reloadData()
        self.cardDelegate?.moreBankCard!(title: (sender.titleLabel?.text)!)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CardCenterView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? SelectBankCell
        cell?.titleLabel.text = dataArray[indexPath.row] + cardCenter
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.cardDelegate?.selectBankType!(title: dataArray[indexPath.row], imageUrl: images[indexPath.row])
    }
    
}
